﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yu.Cms.Common;
using System.Web.Security;
using Yu.Cms.HttpModules;

namespace Yu.Cms.Web.UserCtrl.Admin
{
    public partial class Login : BaseCtrl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string act = YuRequest.GetQueryString("act",true);
                if (act.ToLower() == "login")
                {
                    string getcode = YuRequest.GetFormString("getcode",true);
                    string username = YuRequest.GetFormString("username",true);
                    string userpass = YuRequest.GetFormString("userpass",true);
                    if (!isValidCode(getcode))
                    {
                        Response.Write("2");
                        Response.End();
                    }
                    else if (!isLogin(username, userpass))
                    {
                        Response.Write("1");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("0");
                        Response.End();
                    }
                }
                else if (act.ToLower() == "out")
                {
                    this.isLogout();
                }
            }
        }

        /// <summary>
        /// 判断验证码是否正确
        /// </summary>
        /// <param name="inputCode"></param>
        /// <returns></returns>
        protected bool isValidCode(string inputCode)
        {
            if (this.Session["CaptchaImageText"].ToString().ToLower() == inputCode.ToLower())
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 判断是否登陆
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        protected bool isLogin(string username, string password)
        {
            Yu.Cms.Model.Admin mAdmin = bllAdmin.GetModel(username);
            if (mAdmin != null)
            {
                if (mAdmin.UserName == username)
                {
                    if (mAdmin.Password == Utils.Encrypt(password))
                    {
                        string User = mAdmin.UserName;
                        mAdmin.Lip = mAdmin.Fip;
                        mAdmin.Fip = Utils.GetRealIP();
                        mAdmin.LlDate = mAdmin.FlDate;
                        mAdmin.FlDate = DateTime.Now;
                        bllAdmin.Update(mAdmin);
                        this.SetLogin(User, "-1");
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return false;
        }


        /// <summary>
        /// 登录票据
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="roles"></param>
        public  void SetLogin(string Username, string roles)
        {
            FormsAuthentication.Initialize();
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                2,
                Username,
                DateTime.Now,
                DateTime.Now.AddMinutes(30),
                false,
                roles,
                FormsAuthentication.FormsCookiePath);
            string hashTicket = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
            cookie.HttpOnly = true;
            cookie.Path = FormsAuthentication.FormsCookiePath;
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        protected void isLogout()
        {
            if (HttpContext.Current.Session != null)
                HttpContext.Current.Session.Abandon();
            FormsAuthentication.SignOut();
            //HttpContext.Current.Response.Redirect("~/Admin/Login.aspx");

        }

    }
}